import { Badge, Descriptions, Drawer } from "antd";
import { forwardRef, useImperativeHandle, useMemo, useState } from "react"
import DisplayLabel from "@/components/displayLabel";
import { parseTime } from "@/utils";

let opts = [
  { value: '0', props: { text: '默认策略', status: 'default' } },
  { value: '1', props: { text: '立即执行', status: 'processing' } },
  { value: '2', props: { text: '执行一次', color: 'purple' } },
  { value: '3', props: { text: '放弃执行', status: 'warning' } },
]

let MisfirePolicy = ({ value }) => {
  let opt = opts.find(item => item.value == value)
  if (!opt) return '';
  return <Badge {...opt.props}></Badge>
}


const ViewFormModal = forwardRef((props, ref) => {
  const [fullData, setFullData] = useState({
    jobId: undefined,
    jobName: undefined,
    jobGroup: undefined,
    invokeTarget: undefined,
    cronExpression: undefined,
    misfirePolicy: '1',
    concurrent: '1',
    status: '0'
  })
  const [isModalOpen, setIsModalOpen] = useState()

  const borderedItems = useMemo(() => {
    let r = [
      {
        key: 'jobId',
        label: '任务编号',
        children: fullData.jobId,
      },
      {
        key: 'jobName',
        label: '任务名称',
        children: fullData.jobName,
      },
      {
        key: 'jobGroup',
        label: '任务分组',
        children: <DisplayLabel value={fullData.jobGroup} type={'sys_job_group'} />,
      },
      {
        key: 'createTime',
        label: '创建时间',
        children: fullData.createTime,
      },
      {
        key: 'nextValidTime',
        label: '下次执行时间',
        span: 2,
        children: parseTime(fullData.nextValidTime),
      },
      {
        key: 'invokeTarget',
        label: '调用目标方法',
        children: fullData.invokeTarget,
      },
      {
        key: 'cronExpression',
        label: 'cron表达式',
        span: 2,
        children: fullData.cronExpression,
      },
      {
        key: 'status',
        label: '任务状态',
        children: <Badge status={fullData.status == '0' ? 'processing' : 'warning'} text={<DisplayLabel value={fullData.status} type="sys_job_status" />} />,
      },
      {
        key: 'concurrent',
        label: '是否并发',
        children: <Badge status={fullData.concurrent == '0' ? 'processing' : 'warning'} text={fullData.concurrent == '0' ? '允许' : '禁止'} />,
      },
      {
        key: 'misfirePolicy',
        label: '执行策略',
        children: <MisfirePolicy value={fullData.misfirePolicy} />,
      },

    ]

    return r
  }, [fullData])

  useImperativeHandle(ref, () => ({
    showModal: (data) => {
      setFullData(data)
      setIsModalOpen(true)
    },
  }));

  const handleCancel = () => {
    setIsModalOpen(false)
  };
  return <Drawer width={750} open={isModalOpen} placement="right"
    closable={false} onClose={handleCancel}>
    <Descriptions
      title="任务详细"
      layout="vertical"
      bordered
      column={3}
      items={borderedItems}
    />
  </Drawer>
})

export default ViewFormModal